//
//  CXRingProgressView.swift
//  RNAsmanMaster
//
//  Created by 李旭峰 on 2/13/20.
//  Copyright © 2020 Facebook. All rights reserved.
//

import UIKit

class CXRingProgressView: UIView {
    var marginTop:CGFloat = 5.0
    var marginLeft:CGFloat = 5.0
    fileprivate var isHiddenProgress:Bool = false
    /// 进度条颜色
    var progressColor: UIColor = UIColor(hexStr: "#3271F5", alpha: 1.0) {
        didSet {
            self.progressLayer.strokeColor = progressColor.cgColor
            self.setNeedsDisplay()
        }
    }
    var ringColor:UIColor = UIColor(hexStr: "#3271F5", alpha: 1.0) {
         didSet {
             self.ringLayer.strokeColor = ringColor.cgColor
             self.setNeedsDisplay()
         }
     }
    var ringWidth:CGFloat = 3.0{
        didSet{
            self.progressLayer.lineWidth = self.ringWidth
        }
    }
    private lazy var progressLayer: CAShapeLayer = {
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.clear.cgColor
        layer.strokeColor = UIColor(hexStr: "#3271F5", alpha: 1.0).cgColor
        layer.lineWidth = 3.0
        layer.lineCap = CAShapeLayerLineCap.round
        return layer
    }()

    private lazy var ringLayer: CAShapeLayer = {
        let layer = CAShapeLayer()
        layer.fillColor = UIColor.clear.cgColor
        layer.strokeColor = UIColor(hexStr: "#3271F5", alpha: 1.0).cgColor
        layer.lineWidth = 3.0
        layer.lineCap = CAShapeLayerLineCap.round
        return layer
    }()
    var progress: Float = 0.0 {
        didSet{
            if self.isHiddenProgress {
                self.progress = 0.0
            }
            self.setNeedsDisplay()
        }
    }
    init() {
        super.init(frame: CGRect.zero)
        self.layer.addSublayer(self.ringLayer)
        self.layer.addSublayer(self.progressLayer)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.layer.addSublayer(self.ringLayer)
        self.layer.addSublayer(self.progressLayer)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.addSublayer(self.ringLayer)
        self.layer.addSublayer(self.progressLayer)
    }
    
    func displayProgress(){
        self.isHiddenProgress = false
        self.progressLayer.strokeColor = self.progressColor.cgColor
        self.setNeedsDisplay()
    }
    func hiddenProgress(){
        self.isHiddenProgress = true
        self.progress = 0
        self.progressLayer.strokeColor = UIColor.clear.cgColor
        self.setNeedsDisplay()
    }
    
    override func draw(_ rect: CGRect) {
        let width = self.bounds.width
        let height = self.bounds.height
      
        let ringFrame = CGRect(x: marginLeft, y: marginTop, width: width - marginLeft * 2.0, height: height - marginTop * 2.0)
        let ringPath = UIBezierPath.init(roundedRect: ringFrame, cornerRadius: ringFrame.width/2.0)
        self.ringLayer.path = ringPath.cgPath
        self.ringLayer.strokeEnd = 1
      
        let progressFrame = CGRect(x: marginLeft, y: marginTop, width: width - marginLeft * 2.0, height: height - marginTop * 2.0)
        let progressPath = UIBezierPath.init(roundedRect: progressFrame, cornerRadius: progressFrame.width/2.0)
        self.progressLayer.path = progressPath.cgPath
        self.progressLayer.strokeEnd = CGFloat(self.progress)
    }
}
